/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apt;

import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 *
 * @author PhiNguyen
 */
public class LoginForm extends JFrame {

    private JLabel lblUsername;
    private JLabel lblPassword;
    private JTextField txtUsername;
    private JPasswordField txtPassword;
    private JLabel txtNotice;
    private JButton btnLogin;
    private JButton btnLogout;
    private JLabel lblImage;

    public LoginForm(String title) throws HeadlessException {
        super(title);
        FlowLayout layout = new FlowLayout();
        this.setLayout(layout);

        lblUsername = new JLabel("Username");
        this.add(lblUsername);

        txtUsername = new JTextField(10);
        this.add(txtUsername);

        lblPassword = new JLabel("Password");
        this.add(lblPassword);

        txtPassword = new JPasswordField(10);
        this.add(txtPassword);

        txtNotice = new JLabel("Wellcome, please Login !");
        this.add(txtNotice);

        btnLogin = new JButton("Login");
        btnLogin.setActionCommand("cmdLogin");
        this.add(btnLogin);

        btnLogout = new JButton("Log Out");
        btnLogout.setActionCommand("cmdLogout");
        btnLogout.setVisible(false);
        this.add(btnLogout);

        Icon icon = new ImageIcon(this.getClass().getResource("Snapshot_2012-09-27_141823.png"));

        lblImage = new JLabel(icon);
        lblImage.setVisible(false);
        this.add(lblImage);

        ButtonHandler handler = new ButtonHandler();
        btnLogin.addActionListener(handler);
        btnLogout.addActionListener(handler);
    }

    public class ButtonHandler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            String cmd = e.getActionCommand();

            if (cmd.equals("cmdLogin")) {
                String userName = txtUsername.getText().trim();
                String passWord = txtPassword.getText().trim();

                if (userName.equals("anhphi") && passWord.equals("123")) {
                    txtNotice.setText("Login Success ! Hi " + userName);
                    lblImage.setVisible(true);
                    lblUsername.setVisible(false);
                    txtUsername.setVisible(false);
                    lblPassword.setVisible(false);
                    txtPassword.setVisible(false);
                    btnLogin.setVisible(false);
                    btnLogout.setVisible(true);
                } else {
                    txtNotice.setText("Login Failed ! Pls try again...");
                }

            }
            if (cmd.equals("cmdLogout")) {
                lblImage.setVisible(false);
                lblUsername.setVisible(true);
                txtUsername.setVisible(true);
                lblPassword.setVisible(true);
                txtPassword.setVisible(true);
                btnLogin.setVisible(true);
                btnLogout.setVisible(false);
            }
        }
    }
}
